import Scanner from "./Scanner.js";
import nestTokens from "./nestTokens.js"

export default function parseTemplateToTokens(templateStr) {
    // 实例化一个扫描器，对模板字符串进行解析。
    let scanner = new Scanner(templateStr);
    let tokens = [];
    let words;
    while (!scanner.eos()) {
        words = scanner.scanUntil("{{");
        if (words != "") {
            tokens.push(["text", words]);
        }
        scanner.scan("{{");
        words = scanner.scanUntil("}}");
        if (words != "") {
            // 判断双大括号里面的是否字符
            if(words[0] == '#'){
                tokens.push(["#", words.substring(1)]);
            }else if(words[0] == '/'){
                tokens.push(["/", words.substring(1)]);
            }else{
                tokens.push(["name", words]);
            }
            
        }
        scanner.scan("}}");
    }
    return nestTokens(tokens)
}
